package com.dingreading.cloud.admin.controller;

import com.dingreading.cloud.admin.entity.WebImage;
import com.dingreading.cloud.admin.service.WebImageService;
import com.dingreading.cloud.common.base.R;
import com.dingreading.cloud.common.util.AccountUtil;
import com.dingreading.cloud.common.util.DescribeException;
import com.dingreading.cloud.common.util.PageBeanDto;
import com.dingreading.cloud.common.util.PageUtil;
import com.dingreading.cloud.common.util.aliyun.OSSClientUtil;
import com.mybatisflex.core.paginate.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

@RefreshScope
@RestController
@RequestMapping("/system/webImage")
@Api(tags = "总部-图片管理")
public class WebImageController {

    @Value("${envi}")
    private String envi;

    @Resource
    protected WebImageService webImageService;


    @ApiOperation(value = "分页列表")
    @PostMapping(value = "/pageList", produces = "application/json; charset=UTF-8")
    public R<Object> pageList(
            @ApiParam(value = "分页公共类") PageUtil pageUtil,
            @ApiParam(value = "总部员工uid") @RequestParam(value = "headStaffUid") String headStaffUid,
            @ApiParam(value = "总部员工名") @RequestParam(value = "headStaffName") String headStaffName,
            @ApiParam(value = "启用状态") @RequestParam(value = "enabled", required = false) Integer enabled,
            @ApiParam(value = "图片类型") @RequestParam(value = "typeId", required = false) Integer typeId
    ) {
        Page<WebImage> page = webImageService.pageList(pageUtil, null, enabled, typeId);

        PageBeanDto data = new PageBeanDto();
        data.setTotal(page.getTotalRow());
        data.setList(page.getRecords());
        return R.ok(data);
    }


    @ApiOperation(value = "获取信息")
    @PostMapping(value = "/getBean", produces = "application/json; charset=UTF-8")
    public R<Object> getBean(
            @ApiParam(value = "总部员工uid") @RequestParam(value = "headStaffUid") String headStaffUid,
            @ApiParam(value = "总部员工名") @RequestParam(value = "headStaffName") String headStaffName,
            @ApiParam(value = "仓库id") @RequestParam(value = "id") Long id
    ) {
        WebImage data = this.webImageService.getById(id);
        return R.ok(data);
    }


    @ApiOperation(value = "删除")
    @PostMapping(value = "/dropBean", produces = "application/json; charset=UTF-8")
    public R<Object> dropBean(
            @ApiParam(value = "总部员工uid") @RequestParam(value = "headStaffUid") String headStaffUid,
            @ApiParam(value = "总部员工名") @RequestParam(value = "headStaffName") String headStaffName,
            @ApiParam(value = "仓库id") @RequestParam(value = "id") Long id
    ) {
        WebImage data = this.webImageService.getById(id);
        if (null == data)
            return R.fail("获取失败");

        long l = webImageService.dropBean(id);
        if (l <= 0) return R.failDel();

        // 删除阿里云图片
        deleteOssFile(data.getUrl());

        return R.ok();
    }

    @Transactional(rollbackFor = DescribeException.class)
    @ApiOperation(value = "添加/修改")
    @PostMapping(value = "/newBean", produces = "application/json; charset=UTF-8")
    public R<Object> newBean(
            @ApiParam(value = "总部员工uid") @RequestParam(value = "headStaffUid") String headStaffUid,
            @ApiParam(value = "总部员工名") @RequestParam(value = "headStaffName") String headStaffName,
            @ApiParam(value = "图片id") @RequestParam(value = "id", required = false) Long id,
            @ApiParam(value = "图片类型名称") @RequestParam(value = "type", required = false) String type,
            @ApiParam(value = "图片类型id") @RequestParam(value = "typeId", required = false) Integer typeId,
            @ApiParam(value = "图片地址") @RequestParam(value = "url", required = false) String url
    ) {
        if (StringUtils.isBlank(url))
            return R.fail("请上传图片地址");

        WebImage data = null;
        if (null == id || id <= 0) {
            if (StringUtils.isBlank(type))
                return R.fail("请填写图片分类");
            if (null == typeId)
                return R.fail("请填写图片分类id");
            // 新增
            Integer maxOrderNum = webImageService.getMaxShowOrder(typeId) + 1;

            data = new WebImage();
            data.setAddStaffUid(headStaffUid);
            data.setAddStaffName(headStaffName);
            data.setType(type);
            data.setTypeId(typeId);
            data.setOrderNum(maxOrderNum);
            data.setEnabled(1);
            data.setUrl(url);
            data.setLastStaffUid(headStaffUid);
            data.setLastStaffName(headStaffName);
            webImageService.saveOrUpdate(data);
        } else {
            data = webImageService.getById(id);
            if (null == data) return R.fail("获取失败");

            String exUrl = data.getUrl();

            data.setUrl(url);
            data.setLastStaffUid(headStaffUid);
            data.setLastStaffName(headStaffName);
            webImageService.saveOrUpdate(data);

            // 文件地址不一样，则删除原文件地址的文件
            if (!exUrl.equals(url)) {
                // 删除阿里云图片
                deleteOssFile(exUrl);
            }
        }
        return R.ok();
    }


    @ApiOperation(value = "更新状态")
    @PostMapping(value = "/updateEnabled", produces = "application/json; charset=UTF-8")
    public R<Object> updateEnabled(
            @ApiParam(value = "总部员工uid") @RequestParam(value = "headStaffUid") String headStaffUid,
            @ApiParam(value = "总部员工名") @RequestParam(value = "headStaffName") String headStaffName,
            @ApiParam(value = "仓库id") @RequestParam(value = "id", required = false) Long id,
            @ApiParam(value = "启用状态 1.启用 2.禁用") @RequestParam(value = "enabled") Integer enabled
    ) {
        boolean l = webImageService.updateEnabled(id, enabled);
        if (!l) return R.fail("更新失败");

        return R.ok();
    }


    @ApiOperation(value = "上移/下移")
    @PostMapping(value = "/upOrDown", produces = "application/json; charset=UTF-8")
    public R<Object> upOrDown(
            @ApiParam(value = "总部员工uid") @RequestParam(value = "headStaffUid") String headStaffUid,
            @ApiParam(value = "总部员工名") @RequestParam(value = "headStaffName") String headStaffName,
            @ApiParam(value = "id") @RequestParam(value = "id") Long id,
            @ApiParam(value = "类型 1.上移 2.下移") @RequestParam(value = "type") Integer type
    ) {
        WebImage data = webImageService.getById(id);
        if (data == null) return R.fail("分类获取失败");
        if (type.equals(1)) {
            // 上移
            WebImage preData = webImageService.getLtShowOrder(data.getTypeId(), data.getOrderNum());
            if (preData == null) return R.fail("已在最前");
            // 和前一条记录的序号互换
            Integer prevOrderNum = preData.getOrderNum();
            Integer curOrderNum = data.getOrderNum();
            // 序号互换
            webImageService.updateOrderNum(preData.getId(), curOrderNum);
            webImageService.updateOrderNum(data.getId(), prevOrderNum);
        } else if (type.equals(2)) {
            // 下移
            WebImage backData = webImageService.getGtShowOrder(data.getTypeId(), data.getOrderNum());
            if (backData == null) return R.fail("已在最后");
            // 和后一条记录的序号互换
            Integer backOrderNum = backData.getOrderNum();
            Integer curOrderNum = data.getOrderNum();
            // 序号互换
            webImageService.updateOrderNum(backData.getId(), curOrderNum);
            webImageService.updateOrderNum(data.getId(), backOrderNum);
        } else {
            return R.fail("类型错误");
        }
        return R.ok();
    }

    /**
     * 删除阿里云图片
     */
    private void deleteOssFile(String url) {
        if (AccountUtil.judgeProd(envi)) {
            OSSClientUtil ossClientUtil = new OSSClientUtil();
            ossClientUtil.initOss();
            ossClientUtil.deleteFile(OSSClientUtil.publicBucket, url);
        }
    }
}
